

var rowName='__rowName';
var tableId = "__table";

function getRowValues(){   
    var trArray = document.getElementsByName(rowName);
    var valueArray = [];
    if (trArray){   
        for (var i = 0; i < trArray.length; i++){   
            var tr = trArray[i];   
            var tdArray = tr.getElementsByTagName("td");   
            var index = tdArray[0].innerHTML; 
            var condition = tdArray[1].innerHTML;   
            var expression = tdArray[2].innerHTML;   
            valueArray[i] = {"index": index,"condition":condition,"expression":expression}; 
        }   
    }
    return valueArray;
}   
function fillExpression(){
	var srcEle = event.srcElement;
	var value = srcEle.innerHTML;
	//填充表达式的内容,并记录当前表格对应的坐标
	var expressionElement = document.getElementById("expression");
	expressionElement.disabled = false;
	expressionElement.value=value;
	expressionElement.cellId = srcEle.id;
	expressionElement.expressionType = srcEle.cellIndex;
	var promptDiv = document.getElementById("promptInfo");
	
	if(promptDiv){
		promptDiv.innerHTML = "您当前编辑的优先级为 " + srcEle.parentElement.rowIndex +" 的" + (srcEle.cellIndex == 1 ? "条件":"运算") + "表达式";
	}
	
}
/**
 * 创建一行数据，其中第一列为序号,最后一列为调整列（上下移动及删除）
 * @param {} cellValues
 */  
function addRow(cellValues){   
    //创建行
    var table    = document.getElementById(tableId);   
    var tr = document.createElement('tr');   
    tr.id = rowName;   
    tr.className='dg_alternatingitemstyle';
    tr.title="请点击单元格进行编辑";
    table.appendChild(tr);


    //索引列
    var indexCell = document.createElement('td');   
	tr.appendChild(indexCell);
	indexCell.appendChild(document.createTextNode(table.rows.length));
	//其他的列
   	
    for(var index in cellValues){
    	var td = document.createElement('td');
    	tr.appendChild(td);
    	td.attachEvent('onclick',fillExpression);    	
    	td.appendChild(document.createTextNode(cellValues[index]));
    	td.id = "cell_" + tr.rowIndex + "_" + td.cellIndex;
    }   
    var linkCell = document.createElement('td');   
    tr.appendChild(linkCell);
    var upHref = "<a   href='#'onclick=\"moveRow(this,true);\" >上移</a>";   
    var downHref = "<a href='#' onclick=\"moveRow(this,false);\">下移</a>";   
    var delHref = "<a   href='#'onclick=\"removeRow(this,'您确定要删除该行数据吗?');\">删除</a>";   
    linkCell.innerHTML = upHref + "  " + downHref + "  " + delHref;
    linkCell.align="center";
    
}   
  
/**
 * 移动行
 * @param {} link 行中的链接对象
 * @param {} up 是否是向上移动
 */
function moveRow(link,up){   
    var table      = document.getElementById(tableId);   
    var selectedTr = link.parentElement.parentElement;   
    var sibling      = up ? selectedTr.previousSibling : selectedTr.nextSibling;   
    if (sibling){
    	//更新序号
        var selectedIndex = selectedTr.getElementsByTagName("td")[0].innerHTML;   
        selectedTr.getElementsByTagName("td")[0].innerHTML = sibling.getElementsByTagName("td")[0].innerHTML;   
        sibling.getElementsByTagName("td")[0].innerHTML = selectedIndex;
        if(up){
        	table.insertBefore(selectedTr, sibling);   
        }else{
        	 table.insertBefore(sibling, selectedTr);  
        }
        
    }
}   

  
function removeRow(link,confirmString){   
	if (window.confirm(confirmString)){   
		var table = document.getElementById(tableId);   
	    var selectedTr = link.parentElement.parentElement;
	    //更新序号
	    while(selectedTr.nextSibling){   
	        selectedTr = selectedTr.nextSibling;   
	        selectedTr.getElementsByTagName("td")[0].innerHTML -= 1;   
    	}	   
    	table.removeChild(link.parentElement.parentElement);   
    }   
}   
